package com.kedou.net;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import com.kedou.app.Config;
import com.kedou.app.R;
import com.kedou.domain.User;
import com.kedou.util.Util;

/**
 * 请求后台 获取投资人信息
 * 
 * @author zhouchao
 * 
 */
public class PostFunderInfo {
	public PostFunderInfo(String token, String userId, String funderId,
			String os, String deviceId, final SuccessCallback successCallback,
			final FailCallback failCallback, final Context context) {
		new NetConnection(
				Util.getSDBaseUrl() + Config.URL_FUNDERINFO,
				HttpMethod.POST,
				new NetConnection.SuccessCallback() {

					@Override
					public void onSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							User user = new User();
							switch (obj.getInt(Config.KEY_STATUSCODE)) {
							case Config.RESULT_STATUS_SUCCESS:
								if (obj.has("data")) {
									JSONObject jsonData = obj
											.getJSONObject("data");
									if (jsonData.has("portrait")) {
										user.setPortrait(jsonData
												.getString("portrait"));
									}
									if (jsonData.has("userId")) {
										user.setUserId(jsonData
												.getString("userId"));
									}
									if (jsonData.has("realName")) {
										user.setRealName(jsonData
												.getString("realName"));
									}
								}
								successCallback.onSuccess(
										obj.getString(Config.KEY_STATUSMSG),
										user);
								break;
							default:
								if (failCallback != null) {
									failCallback.onFail(obj
											.getString(Config.KEY_STATUSMSG));
								}
								break;
							}

						} catch (JSONException e) {
							e.printStackTrace();
							if (failCallback != null) {
								failCallback.onFail(context.getResources()
										.getString(R.string.server_have_wrong));
							}
						}
					}
				}, new NetConnection.FailCallback() {

					@Override
					public void onFail() {
						if (failCallback != null) {
							failCallback.onFail(context.getResources()
									.getString(R.string.server_have_wrong));
						}
					}
				}, Config.KEY_TOKEN, token, Config.KEY_USERID, userId,
				Config.KEY_FUNDERID, funderId + "", Config.KEY_OS, os,
				Config.KEY_DEVICE, deviceId);
	}

	public static interface SuccessCallback {
		void onSuccess(String msg, User user);
	}

	public static interface FailCallback {
		void onFail(String msg);
	}
}
